package com.ktwlsoft.fundmanage.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Spring Mvc 相关配置
 * 
 * @author 何祖文
 * @ModifiedBy 兰坚
 * @time 2019年3月20日 上午11:51:07
 *
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

	@Bean
	public HttpMessageConverter<String> responseBodyConverter() {
		return new StringHttpMessageConverter(Charset.forName("UTF-8"));
	}

	// @Override
	// protected void addCorsMappings(CorsRegistry registry) {
	// registry.addMapping("/**").allowedOrigins("*")
	// .allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH",
	// "DELETE").maxAge(MAX_AGE_SECS);
	// }

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(getMappingJackson2HttpMessageConverter());
		// 这里必须加上加载默认转换器，不然bug玩死人，并且该bug目前在网络上似乎没有解决方案
		converters.add(responseBodyConverter());
		// 百度，谷歌，各大论坛等。你可以试试去掉。
		addDefaultHttpMessageConverters(converters);
	}

	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		configurer.favorPathExtension(false);
	}

	/**
	 * 时间转换
	 *
	 * @author 未知
	 * @return MappingJackson2HttpMessageConverter 2019年3月20日 上午11:53:26
	 */
	@Bean
	public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
		// 设置日期格式
		CustomObjectMapper objectMapper = new CustomObjectMapper();
		objectMapper.setDateFormat(StdDateFormat.INSTANCE);
		mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
		// 设置中文编码格式
		List<MediaType> list = new ArrayList<MediaType>();
		list.add(MediaType.APPLICATION_JSON_UTF8);
		mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
		return mappingJackson2HttpMessageConverter;
	}

	/**
	 * 修改springboot中默认的静态文件路径
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// addResourceHandler请求路径
		// addResourceLocations 在项目中的资源路径
		// setCacheControl 设置静态资源缓存时间
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/")
				// 开发时让缓存去死..
				.setCacheControl(CacheControl.maxAge(1, TimeUnit.SECONDS).cachePublic());
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		super.addResourceHandlers(registry);
	}
}
